{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python基础教学代码---基础篇  \n",
    "1.变量赋值  \n",
    "2.标准数据类型  \n",
    "3.数据转换  \n",
    "4.算数运算符  \n",
    "5.格式化\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.变量赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2 2\n"
     ]
    }
   ],
   "source": [
    "a = 1  # 单变量赋值\n",
    "c = b = 2  # 多变量赋值\n",
    "print(a)\n",
    "print(b, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'float'>\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "# 变量类型\n",
    "name = 'Chile'  # 字符串\n",
    "miles = 1000.0  # 浮点型\n",
    "num = 100  # 整形\n",
    "# 打印变量类型\n",
    "print(type(name))\n",
    "print(type(miles))\n",
    "print(type(num))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***\n",
    "## 2.标准数据类型  \n",
    "\n",
    "Python有6个标准的数据类型：  \n",
    "1.Numbers（数字）  \n",
    "2.String（字符串）  \n",
    "3.List（列表）  \n",
    "4.Tuple（元组）  \n",
    "5.Dictionary（字典）  \n",
    "6.Set(集合)  \n",
    "其中List，Tuple，Dictionary,Set可以放任意数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Numbers: int & float\n",
    "a = 1  # int\n",
    "b = 1.0  # float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C\n",
      "hi\n",
      "ile\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "# String\n",
    "my_name = 'Chile'\n",
    "print(my_name[0])  # 打印第0个字符\n",
    "print(my_name[1: 3])  # 打印第1个到第2个的字符\n",
    "print(my_name[2:])  # 打印第2个到最后一个的字符\n",
    "print(my_name[-1])  # 打印倒数第一个字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "['Chile', 'b', 'c']\n",
      "['a', 1, 1.0, [1, 2, 3, 4], ['Chile', 'b', 'c']]\n"
     ]
    }
   ],
   "source": [
    "# List 可以放任意类型的数据类型\n",
    "num_list = [1, 2, 3, 4]\n",
    "str_list = ['Chile', 'b', 'c']\n",
    "mix_list = ['a', 1, 1.0, num_list, str_list]\n",
    "print(num_list)\n",
    "print(str_list)\n",
    "print(mix_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "('chile', 111, 2.2, 'a', [1, 2, 3, 4])\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-26-594ace6ddd44>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmix_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmix_tuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mmix_tuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m  \u001b[0;31m# 不可赋值，否则报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# Tuple 可以放任意类型的数据类型\n",
    "mix_tuple = ('chile', 111, 2.2, 'a', num_list)  # 不可赋值\n",
    "print(mix_list[1])\n",
    "print(mix_tuple)\n",
    "mix_tuple[1] = 1  # 不可赋值，否则报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Chile', 'age': 18, 'num_list': [1, 2, 3, 4], 'tuple': ('chile', 111, 2.2, 'a', [1, 2, 3, 4])}\n",
      "dict_keys(['name', 'age', 'num_list', 'tuple'])\n",
      "dict_values(['Chile', 18, [1, 2, 3, 4], ('chile', 111, 2.2, 'a', [1, 2, 3, 4])])\n",
      "Chile\n",
      "[1, 2, 3, 4]\n",
      "('chile', 111, 2.2, 'a', [1, 2, 3, 4])\n"
     ]
    }
   ],
   "source": [
    "# Dictionary 可以放任意类型的数据类型\n",
    "test_dict = {'name': 'Chile', 'age': 18, 'num_list': num_list, 'tuple': mix_tuple}\n",
    "print(test_dict)\n",
    "print(test_dict.keys())  # 打印键\n",
    "print(test_dict.values())  # 打印值\n",
    "print(test_dict['name'])\n",
    "print(test_dict['num_list'])\n",
    "print(test_dict['tuple'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典的赋值“陷阱”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alialili\n",
      "alialili\n"
     ]
    }
   ],
   "source": [
    "# 直接字典赋值， 被赋值的字典的值改变，原字典也会改变\n",
    "test_dict_copy = test_dict\n",
    "test_dict_copy['name'] = 'alialili'\n",
    "print(test_dict['name']) \n",
    "print(test_dict_copy['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alialili\n",
      "Mary\n"
     ]
    }
   ],
   "source": [
    "# 使用深拷贝避免这种情况发生\n",
    "from copy import deepcopy\n",
    "test_dict_copy =  deepcopy(test_dict)\n",
    "test_dict_copy['name'] = 'Mary'\n",
    "print(test_dict['name']) \n",
    "print(test_dict_copy['name'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 'chile', '1', 'abc'}\n"
     ]
    }
   ],
   "source": [
    "# 可以放任意类型的基础数据类型\n",
    "# Set 集合：与数学意义上的集合意义一致，集合内每一个值都是唯一的\n",
    "test_set = {'abc', 1, 1, '1', 'chile'}  \n",
    "print(test_set)  # 打印出来会只有一个数字1，因为集合的去重功能"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***\n",
    "## 3.数据类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'int'>\n",
      "<class 'str'>\n",
      "<class 'float'>\n",
      "---------------\n",
      "<class 'list'>\n",
      "<class 'set'>\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "tr_a = '1'\n",
    "int_b = int(tr_a)   # 字符串转数字\n",
    "str_c = str(int_b)   #  数字转字符串\n",
    "float_d = float(str_c)  # 字符串转浮点\n",
    "print(type(tr_a))\n",
    "print(type(int_b))\n",
    "print(type(str_c))\n",
    "print(type(float_d))\n",
    "print('---------------')\n",
    "tr_list = [1, 2, 3]\n",
    "set_a = set(tr_list)  # 列表转集合\n",
    "list_b = list(set_a)  # 集合转列表\n",
    "print(type(tr_list))\n",
    "print(type(set_a))\n",
    "print(type(list_b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***\n",
    "## 4.算数运算符  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c: 1\n",
      "d: 8\n",
      "e: 8.0\n",
      "f: 0\n",
      "g: 1\n",
      "h: 8\n"
     ]
    }
   ],
   "source": [
    "# 运算符\n",
    "a = 2\n",
    "b = a + 2\n",
    "c = a - 1\n",
    "d = a * b\n",
    "e = d / c\n",
    "f = d % c  # 取余\n",
    "g = 3 // 2  # 整除(向下取整)\n",
    "h = 2**3  # 求幂\n",
    "print('c:', c)\n",
    "print('d:', d)\n",
    "print('e:', e)\n",
    "print('f:', f)\n",
    "print('g:', g)\n",
    "print('h:', h)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***\n",
    "## 5.格式化\n",
    "%s 代表字符串  \n",
    "%d 代表整数  \n",
    "%f 代表浮点  \n",
    "%.2f 代表保留小数点后两位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc 1, dhfjdhfhdh, Chile, sjdhsjhdhs, skdjskjsk1.000000,sdjsdhs\n",
      "abc 1, dhfjdhfhdh, Chile, sjdhsjhdhs, skdjskjsk1.00,sdjsdhs\n"
     ]
    }
   ],
   "source": [
    "# 格式化\n",
    "print('abc %d, dhfjdhfhdh, %s, sjdhsjhdhs, skdjskjsk%f,sdjsdhs' % (1, 'Chile', 1.0))\n",
    "# %.2f保留小数点\n",
    "print('abc %d, dhfjdhfhdh, %s, sjdhsjhdhs, skdjskjsk%.2f,sdjsdhs' % (1, 'Chile', 1.0))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
